﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using CoolCode;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Web.Mvc;
using Kangxin.Mvc.Models;

namespace Kangxin.Mvc.Controllers
{
    [HandleError]
    public class HomeController : CoolCode.Web.Mvc.ControllerBase
    {
        #region Services
        private ModuleList _menuData;
        public ModuleList MenuData
        {
            get
            {
                if (_menuData == null)
                {
                    _menuData = MenuService.ListMenu(this.User.Identity.Name);
                }
                return _menuData;
            }
        }

        private IMenuService _MenuService;
        public IMenuService MenuService
        {
            get
            {
                if (_MenuService == null)
                    _MenuService = Digger.Dig<IMenuService>();
                return _MenuService;
            }
        }

        #endregion

        public HomeController()
        {
        }

        public HomeController(IMenuService _menuService)
        {
            this._MenuService = _menuService;
        }

        public ActionResult Index()
        {
            if (!User.Identity.IsAuthenticated)
                return RedirectToAction("Login");
            return View(); 
        }

        public ActionResult Menu()
        {
            return View();
        }

        public ActionResult Header()
        {
            return View();
        }

        public ActionResult Footer()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult LoadMenu()
        {
            TreeItem root = new TreeItem
            {
                id = 0,
                checkstate = 0,
                complete = true,
                isexpand = true,
                showcheck = true,
                text = "所有项目",
                value = ""
            };
            foreach (var menu in MenuData)
            {
                TreeItem item = new TreeItem { id = menu.Seq, text = menu.Title, value = Url.Action(menu.Name, menu.ModuleId), isexpand = true, complete = true };
                foreach (var submenu in menu.Children)
                {
                    TreeItem t = new TreeItem() { id = submenu.Seq, text = submenu.Title, value = Url.Action(submenu.Name, submenu.ModuleId), isexpand = false, complete = true };
                    item.Add(t);
                }
                item.ChildNodes[item.ChildNodes.Count - 1].complete = true;
                root.Add(item);
            }
            root.ChildNodes[root.ChildNodes.Count - 1].complete = true;
            return Json(root.ChildNodes, JsonRequestBehavior.AllowGet);
        }

        private string buildUrl(string module, string pageId)
        {
            return Url.Action(pageId, module);
        }

        public ActionResult Login()
        {
            Title = "用户登录";
            LoginModel model = new LoginModel
            {
                SubTitle = Config.CompanyName,
                RememberMe = false
            };
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ValidateUser(LoginModel model, string returnUrl)
        {
            bool success = MembershipService.ValidateUser(model.UserName, model.Password);
            System.Threading.Thread.Sleep(new Random().Next(4000));
            if (success)
            {
                FormsAuthenticationService.SignIn(model.UserName, model.RememberMe);
                this.TrackService.LogAndSubmit(this.BuildLog(UserAction.Access, model.UserName + " Login"));
                return this.Message(success);
            }
            else
            {
                return this.Message(success, "用户名或密码错误！");
            }
        }
    }

}
